package com.audiocn.poster;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

import com.audiocn.Utils.NoteSQL;
import com.audiocn.poster.R;

public class PresentShowDC extends Activity {
	private int nid=0;
	private final int mEdit=Menu.FIRST;
	private final int mChange=Menu.FIRST+1;
	private final int mClose=Menu.FIRST+2;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		nid = getIntent().getIntExtra("dedit", 0);
		final Boolean isEdit = (nid) > 0;
		setContentView(R.layout.show);
		TextView show = (TextView)findViewById(R.id.show);
	
		
		
		if(isEdit){
			Cursor c = NoteSQL.selectOne(this, nid);
			c.moveToFirst();
			while(!c.isAfterLast()){
				setTitle(c.getShort(0)+"--礼品接收人："+c.getString(2));
				show.setText(c.getShort(1));
				c.moveToNext();
			} 
		}else {
			setTitle(getResources().getString(R.string.app_name));
			show.setText("欢迎使用礼品接力");
		}
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, mEdit, Menu.NONE, "修改").setIcon(R.drawable.edit);
		menu.add(0, mChange, Menu.NONE, "转换屏幕").setIcon(R.drawable.refresh);
		menu.add(0, mClose, Menu.NONE, "关闭").setIcon(R.drawable.close);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()){
		case mEdit:
			Intent i = new Intent(PresentShowDC.this, PresentEditDC.class);
			i.putExtra("dedit", nid);
			startActivity(i);
			finish();
			return true;
		case mChange:
			changeSreen();
			return true;
		case mClose:
			finish();
			return true;
		default:
			return false;
		}
	}
	
	private void changeSreen(){
		if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}else if(getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
	}
}
